package cn.zjyj.module.zjyj.service.device;

import cn.hutool.core.collection.CollUtil;
import cn.zjyj.module.zjyj.dal.bo.device.DeviceBo;
import cn.zjyj.module.zjyj.dal.dataobject.supplier.SupplierDO;
import cn.zjyj.module.zjyj.dal.mysql.supplier.SupplierMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import cn.zjyj.module.zjyj.controller.admin.device.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.device.DeviceDO;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;

import cn.zjyj.module.zjyj.dal.mysql.device.DeviceMapper;

import static cn.zjyj.framework.common.common.exception.util.ServiceExceptionUtil.exception;
import static cn.zjyj.framework.common.common.util.collection.CollectionUtils.convertList;
import static cn.zjyj.module.zjyj.enums.ErrorCodeConstants.*;

/**
 * 设备 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class DeviceServiceImpl implements DeviceService {

    @Resource
    private DeviceMapper deviceMapper;

    @Resource
    SupplierMapper supplierMapper;
    @Override
    public Long createDevice(DeviceSaveReqVO createReqVO) {
        // 插入
        DeviceDO device = BeanUtils.toBean(createReqVO, DeviceDO.class);
        deviceMapper.insert(device);
        // 返回
        return device.getId();
    }

    @Override
    public void updateDevice(DeviceSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceExists(updateReqVO.getId());
        // 更新
        DeviceDO updateObj = BeanUtils.toBean(updateReqVO, DeviceDO.class);
        deviceMapper.updateById(updateObj);
    }

    @Override
    public void deleteDevice(Long id) {
        // 校验存在
        validateDeviceExists(id);
        // 删除
        deviceMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceExists(ids);
        // 删除
        deviceMapper.deleteByIds(ids);
        }

    private void validateDeviceExists(List<Long> ids) {
        List<DeviceDO> list = deviceMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_NOT_EXISTS);
        }
    }

    private void validateDeviceExists(Long id) {
        if (deviceMapper.selectById(id) == null) {
            throw exception(DEVICE_NOT_EXISTS);
        }
    }

    @Override
    public DeviceDO getDevice(Long id) {
        return deviceMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceBo> getDevicePage(DevicePageReqVO pageReqVO) {
        PageResult<DeviceDO> deviceDOPageResult = deviceMapper.selectPage(pageReqVO);
        PageResult<DeviceBo> bean = BeanUtils.toBean(deviceDOPageResult, DeviceBo.class);
        List<DeviceBo> list = bean.getList();
        if (CollUtil.isEmpty(list)) {
            return bean;
        }
        List<Long> supperIds = list.stream().map(DeviceDO::getSupplierId).collect(Collectors.toList());
        List<SupplierDO> supplierDOS = supplierMapper.selectByIds(supperIds);
        Map<Long, SupplierDO> supplierMap = supplierDOS.stream().collect(Collectors.toMap(SupplierDO::getId, supplierDO -> supplierDO));
        for (DeviceBo deviceBo : list) {
            deviceBo.setSupplierName(supplierMap.get(deviceBo.getSupplierId()).getName());
        }
        bean.setList(list);
        return bean;
    }

}